﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication5.settings;

namespace WindowsFormsApplication5
{
    public partial class MainForm : Form
    {
        
        private Datablock dataBlock;
        private PortConfig[] port_configurations;
        
        public MainForm()
        {
            InitializeComponent();
            dataBlock = new Datablock();
            port_configurations = new PortConfig[4];
            initDebugValues();
        }

        public void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog opendialog = new FolderBrowserDialog();

            string savepath = "C:\\";
            
            // Grundkonfiguration
            dataBlock.router_config = new Routerconfig(hostname_textBox.Text, motd_textBox.Text, console_pw_checkBox.Checked, console_pw_textBox.Text, telnet_pw_checkBox.Checked, telnet_pw_textBox.Text, user_pw_checkBox.Checked, user_name_textBox.Text, user_pw_textBox.Text);
            

            // Schnittstellen
            //dataBlock.port_config[0] = new PortConfig(interface_comboBox.Text, interface_checkBox.Checked, interface_description_textBox.Text, interface_ip_textBox.Text, interface_subnetmask_textBox.Text, interface_clockrate_comboBox.Text);
            dataBlock.port_config = port_configurations;


            // Routing
            //dataBlock.static_router_config = new Static_Routerconfig(defaultroute_radioButton.Checked, staticroute_radioButton.Checked, static_ip_textBox.Text, static_subnetmask_textBox.Text, static_nexthopip_textBox.Text);
            List<Static_Routerconfig> router_configurations = new List<Static_Routerconfig>();
            for (int i = 0; i < routing_grid.RowCount; i++)
            {
                DataGridViewCellCollection cells = routing_grid.Rows[i].Cells;

                bool defaultroute = cells[0] != null && cells[0].Value != null && (Boolean)cells[0].Value;
                String ip = (String)cells[1].Value;
                String hop = (String)cells[2].Value;
                String subnet = (String)cells[3].Value;
                Static_Routerconfig config = new Static_Routerconfig(defaultroute, !defaultroute, ip, subnet, hop);

                router_configurations.Add(config);
            }
            dataBlock.static_router_configurations = router_configurations;

            // RIPv2
            List<String> networkIpList = new List<String>();
            for (int i = 0; i < RIPv2_grid.RowCount; i++)
            {
                DataGridViewCellCollection cells = RIPv2_grid.Rows[i].Cells;
                String networkIp = (String)cells[0].Value;
                if (networkIp != null && networkIp != "")
                {
                    networkIpList.Add(networkIp);
                }
            }
            dataBlock.rip_config = new Ripv2_Routerconfig(networkIpList, RIPv2_routingprotocol_checkBox.Checked, RIPv2_autocomplete_checkBox.Checked);
            
            // OSPF
            List<String> ospf_ipsnWildcards = new List<String>();
            for (int i = 0; i < OSPF_grid.RowCount; i++)
            {
                String s = (String)OSPF_grid.Rows[i].Cells[0].Value + " " + (String)OSPF_grid.Rows[i].Cells[1].Value + " area " + (String)OSPF_grid.Rows[i].Cells[2].Value;
                ospf_ipsnWildcards.Add(s);
            }
            dataBlock.ospf_config = new Ospf_Routerconfig(OSPF_authentification_textBox.Text, ospf_ipsnWildcards, OSPF_routingprotocol_checkBox.Checked, OSPF_processID_textBox.Text);

            if (opendialog.ShowDialog() == DialogResult.OK)
            {
                savepath = opendialog.SelectedPath.ToString();

                String hostname = hostname_textBox.Text;
                hostname = "1";

                if (hostname != "")
                {
                    sheets.DataWriter config = new sheets.DataWriter();
                    config.writeData(savepath, hostname, dataBlock);
                    MessageBox.Show("Done", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("insert hostname", "insert hostname", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void port_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //interface_comboBox.Text = String.Empty;
            interface_checkBox.Checked = false;
            interface_description_textBox.Text = String.Empty;
            interface_ip_textBox.Text = String.Empty;
            interface_subnetmask_textBox.Text = String.Empty;
            interface_clockrate_comboBox.SelectedIndex = 0;

            if (sender is System.Windows.Forms.ComboBox)
            {
                System.Windows.Forms.ComboBox box = (System.Windows.Forms.ComboBox)sender;
                if ((box.Text == "2 FastEthernet Schnittstelle") || (box.Text == "1 FastEthernet Schnittstelle"))
                {
                    interface_clockrate_label.Visible = false;
                    interface_clockrate_comboBox.Visible = false;
                }
                else
                {
                    interface_clockrate_label.Visible = true;
                    interface_clockrate_comboBox.Visible = true;
                }
                switch (box.Text)
                {
                    case "1 FastEthernet Schnittstelle":
                        if (port_configurations[2] != null)
                        {
                            fillPortConfig(port_configurations[2]);
                        }
                        break;
                    case "2 FastEthernet Schnittstelle":
                        if (port_configurations[3] != null)
                        {
                            fillPortConfig(port_configurations[3]);
                        }
                        break;

                    case "1 Serielle Schnittstelle":
                        if (port_configurations[0] != null)
                        {
                            fillPortConfig(port_configurations[0]);
                        }
                        break;

                    case "2 Serielle Schnittstelle":
                        if (port_configurations[1] != null)
                        {
                            fillPortConfig(port_configurations[1]);
                        }
                        break;

                    default:
                        break;
                }
            }

        }

        private void fillPortConfig(PortConfig config)
        {

            interface_checkBox.Checked = config.Port_Enabled;
            interface_description_textBox.Text = config.Port_Description;
            interface_ip_textBox.Text =config.Ip;
            interface_subnetmask_textBox.Text = config.SubnetMask;
            interface_clockrate_comboBox.Text = config.Clockrate;
        }

        private void port_comboBox_Click(object sender, EventArgs e)
        {
            if (sender is System.Windows.Forms.ComboBox)
            {

                System.Windows.Forms.ComboBox box = (System.Windows.Forms.ComboBox)sender;
                if (box.Text == "2 FastEthernet Schnittstelle")
                {
                    savePortConfig(3);
                }
                else if (box.Text == "1 FastEthernet Schnittstelle")
                {
                    savePortConfig(2);
                }
                else if (box.Text == "2 Serielle Schnittstelle")
                {
                    savePortConfig(1);
                }
                else if (box.Text == "1 Serielle Schnittstelle")
                {
                    savePortConfig(0);
                }

            }
        }

        public void savePortConfig(int num)
        {
            port_configurations[num] = new PortConfig(num, interface_checkBox.Checked, interface_description_textBox.Text, interface_ip_textBox.Text, interface_subnetmask_textBox.Text, interface_clockrate_comboBox.Text);

        }

        private void static_button_add_Click(object sender, EventArgs e)
        {

            //ListViewItem item = new ListViewItem();
            //item.SubItems.Add(defaultroute_radioButton.Checked ? "1" : "0");
            //item.SubItems.Add(static_ip_textBox.Text);
            //item.SubItems.Add(static_nexthopip_textBox.Text);
            //item.SubItems.Add(static_subnetmask_textBox.Text);

            routing_grid.Rows.Add(defaultroute_radioButton.Checked, static_ip_textBox.Text, static_nexthopip_textBox.Text, static_subnetmask_textBox.Text);



            //static_listview.Items.Add(defaultroute_radioButton.Checked + ";" + static_ip_textBox.Text + ";" + static_subnetmask_textBox.Text + ";" + static_nexthopip_textBox.Text);
            //new Static_Routerconfig(defaultroute_radioButton.Checked, staticroute_radioButton.Checked, static_ip_textBox.Text, static_subnetmask_textBox.Text, static_nexthopip_textBox.Text);
        }

        private void grid_statisch_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (routing_grid.Rows[e.RowIndex].Cells[0].Value != null 
                && ((Boolean)routing_grid.Rows[e.RowIndex].Cells[0].Value) == true 
                && routing_grid.RowCount > 1)
            {
                for (int i = 0; i < routing_grid.RowCount; i++)
                {
                    if (i != e.RowIndex
                        && routing_grid.Rows[i].Cells[0].Value != null
                        && ((Boolean)routing_grid.Rows[i].Cells[0].Value) == true)
                    {
                        routing_grid.Rows[i].Cells[0].Value = false;
                    }
                }
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void hilfeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO Dialog
        }

        private void initDebugValues()
        {
            user_pw_textBox.Text = "myUserPW";
            telnet_pw_checkBox.Checked = true;
            console_pw_checkBox.Checked = true;
            telnet_pw_textBox.Text = "myTelnetPW";
            console_pw_textBox.Text = "myKonsolePW";
            motd_textBox.Text = "myMotD";
            user_pw_checkBox.Checked = true;
            hostname_textBox.Text = "myHostname";
            interface_ip_textBox.Text = "1.1.1.1";
            interface_checkBox.Checked = true;
            interface_description_textBox.Text = "myIDescription";
            static_nexthopip_textBox.Text = "2.2.2.2";
            static_subnetmask_textBox.Text = "255.255.255.255";
            defaultroute_radioButton.Checked = true;    // RadioButton
            //staticroute_radioButton;        // RadioButton
            static_ip_textBox.Text = "1.2.3.4";
            RIPv2_autocomplete_checkBox.Checked = true;
            RIPv2_routingprotocol_checkBox.Checked = true;
            OSPF_routingprotocol_checkBox.Checked = true;
            OSPF_authentification_textBox.Text = "myOSPFAuth";
            interface_subnetmask_textBox.Text = "255.255.255.255";
            user_name_textBox.Text = "myUserName";
            OSPF_processID_textBox.Text = "999";

            routing_grid.Rows.Add();
            DataGridViewRow rows = routing_grid.Rows[0];            
            rows.Cells[0].Value = true;
            rows.Cells[1].Value = "192.168.100.1";
            rows.Cells[2].Value = "192.168.200.2";
            rows.Cells[3].Value = "255.255.0.0";

            
        }


    }
}
